//
//  CTEditImageViewController.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/5/15.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTEditImageViewController.h"
#import "CTPublishImageViewController.h"

//view
#import "CTEditeImageView.h"

//logic
#import "CTPublishLogic.h"

//function
#import "CTDebugManager.h"
#import "CTGenRequestData.h"
#import "CTDataGenerator.h"
#import "MobClick.h"

@interface CTEditImageViewController ()<CTEditeImageViewDelegate>
{
    CTEditeImageView * editImageView;
    CTPublishLogic * publishLogic;
}
@end

@implementation CTEditImageViewController
@synthesize editImage;

- (void)dealloc
{
    [editImageView removeFromSuperview];
    editImageView = nil;
    editImageView.delegate = nil;
    
    publishLogic = nil;
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTEditImageViewController Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        publishLogic = [[CTPublishLogic alloc] init];
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //统计
    [MobClick beginLogPageView:@"CTEditImageViewController"];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    //统计
    [MobClick endLogPageView:@"CTEditImageViewController"];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    editImageView = [[CTEditeImageView alloc] init];
    editImageView.delegate = self;
    [self.view addSubview:editImageView];
    
    editImageView.editImg = editImage;
    
    [self sendEmojiListRequest];
    [self sendBubbleListRequest];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




#pragma mark - request -

-(void)sendEmojiListRequest
{
    [publishLogic emojiListRequest:[CTGenRequestData genRequestEditeEmojiListItem] successCompletionHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            editImageView.editArray = [CTDataGenerator generateEditeEmojiListItems];
        });
    } failedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            editImageView.editArray = [CTDataGenerator generateEditeEmojiListItems];
        });
    }];
}

-(void)sendBubbleListRequest
{
    editImageView.bubblesArr = [CTDataGenerator generateDefaultBubbleEmojiListItems];
    
    [publishLogic bubbleListRequest:[CTGenRequestData genRequestEditeBubbleListItem] successCompletionHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSArray * bubblesArray = [CTDataGenerator generateBubbleEmojiListItems];
            
            NSArray * defaultBubblesArray = [CTDataGenerator generateDefaultBubbleEmojiListItems];
            NSMutableArray * defaultMutableBubblesArray = [defaultBubblesArray mutableCopy];
            for (CTEditeBubbleListItem * bubbleItem in bubblesArray) {
                [defaultMutableBubblesArray addObject:bubbleItem];
            }
            bubblesArray = [defaultMutableBubblesArray copy];
            defaultMutableBubblesArray = nil;
            defaultBubblesArray = nil;
            
            editImageView.bubblesArr = bubblesArray;
        });
    } failedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            editImageView.bubblesArr = [CTDataGenerator generateBubbleEmojiListItems];
        });
    }];
}


#pragma mark - view delegate -

-(void)nextStepAction:(CTEditeImageView *)view withImage:(UIImage *)img withTopAry:(NSArray *)topicAry withGroupAry:(NSArray *)grounAry withSchemeAry:(NSArray *)schemeAry withWaterImage:(UIImage *)waterImg
{
    CTPublishImageViewController * publishImageViewVC = [[CTPublishImageViewController alloc] init];
    publishImageViewVC.editImage = img;
    publishImageViewVC.waterImage = waterImg;
    publishImageViewVC.topicGroupStr = [publishLogic jsonStrForTopic];
    publishImageViewVC.groupAry = [[publishLogic topicGroupList] copy];
    
    [self.navigationController pushViewController:publishImageViewVC animated:YES];
}

-(void)backAction:(CTEditeImageView *)view
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)selectedEmojiSubjects:(CTEditeImageView*)view withItem:(CTEditeEmojiListItem*)item
{
    [publishLogic setTopicGroupItem:item];
}
- (void)selectedScheme:(CTEditeImageView*)view withItem:(CTEditeEmojiListDetailsItem*)item
{
    [publishLogic setTopicGroupDetailItem:item];
}
- (void)selectedCloseBtn:(CTEditeImageView *)view withItem:(CTEditeEmojiListDetailsItem*)item
{
    [publishLogic delTopicGroupDetailItem:item];
}

@end
